define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Logistics_Modal, $window, GoPage, GoDialog, $timeout) {
		$scope.input = new Object();
		$scope.input.nPage = 1;
		$scope.count = null;
		//页面激活
		$scope.$on('$ionicView.afterEnter', function() {
				var lan = 0; //这是状态栏，只有ios的APP才有
		var tab = 0;
		var bars = document.getElementById("nav-bar-title");
		var bar = ((bars != undefined) ? 44 : 0);
		if(window.cordova && window.cordova.plugins && ionic.Platform.isIOS()) { lan = 20; }
		//整体高度
		var all = $window.innerHeight;
		var logistic_top = document.getElementById("logistic-top").clientHeight;
		$scope.logistic_myObj = { //滚动条可视范围
			"height": all - lan - bar - tab - logistic_top + "px"
		};
		$scope.init_data();
		}, false);
		$scope.isLoad = false;
		//		GoPage.init_top($scope, 0, buttom,0);

		$scope.init_data = function() {
			Logistics_Modal.carriage_list(1, function(data) {
				if(data != null)
					$scope.datas = data;
				else
					$scope.datas = new Object();
				$scope.input.nPage = 1;
			})
		}
		
		//添加
		$scope.doadd = function() {
			GoPage.go_states('logistics-edit');
		};
		//编辑
		$scope.doedit = function(id) {
			GoPage.go_states('logistics-edit', {
				id: id
			});
		};
		//删除
		$scope.dodel = function(index) {
//			console.log(index);

			GoDialog.confirm('确认', '确认要删除该条数据吗？', function() {
				Logistics_Modal.model_delete(index, function(data) {
//					console.log(data);
					if(data.success == true) {
						GoDialog.toast('删除成功');
						$scope.init_data();
					} else
						GoDialog.toast('删除失败');
				});
			});
		};
		//防止重复加载
		//		$scope.isLoad = false;
		//下拉刷新
		$scope.doRefresh = function() {
			$scope.isLoad = true;
			//条件查询
			Logistics_Modal.carriage_list(1, function(data) {
				if(data != null) {
					$scope.datas = data;
					$scope.input.nPage = 1;
				}
				if(data == null) {
					$scope.data = new Object();
					$scope.input.nPage = 1;
				}
			}, function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false; //可以加载
				}, 1500);
			});
		};
		//加载更多
		$scope.doRefreshMore = function() {
			//防止重复加载
			$scope.isLoad = true;
			if($scope.datas.freight == undefined) {
				$scope.input.nPage = 0;

			} else
			if($scope.datas.page.count <= $scope.datas.freight.length) {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				if($scope.datas.page.count<$scope.input.nPage*15){
					$scope.input.nPage -=1;
					$scope.count = $scope.input.nPage *15;
				}else{
					$scope.count = null;
				}
			}

			//条件查询
			Logistics_Modal.carriage_list($scope.input.nPage + 1, function(data) {
				if(data.freight.length > 0) {
					//数据合并
					if($scope.datas.freight != undefined) {
						if($scope.count!=null)
						$scope.datas.freight.splice($scope.count,$scope.datas.page.count-$scope.count);
						$scope.datas.freight = $scope.datas.freight.concat(data.freight);
						$scope.input.nPage = $scope.input.nPage + 1;
					} else {
						$scope.datas = new Object();
						$scope.input.nPage = 1;
					}

				}
			}, function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
					$scope.isLoad = false; //可以加载
				}, 1500);
			});
		};
		//		$scope.init_data();
	}

	controller.$inject = ['$scope', 'Logistics_Modal', '$window', 'GoPage', 'GoDialog', '$timeout'];

	return app.registerController("logistics_modalctrl",controller);
});